package org.igeek.atomqq;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.igeek.atomqq.domain.Friend;
import org.igeek.atomqq.domain.FriendCategory;
import org.igeek.atomqq.domain.Group;
import org.igeek.atomqq.domain.User;
import org.igeek.atomqq.service.MessageReceiver;
import org.igeek.atomqq.service.MessageReceiver.OnMessageRecevied;
import org.igeek.atomqq.task.MessagePoll;
import org.igeek.atomqq.vo.Chat;
import org.igeek.atomqq.vo.QQMessage;
import org.igeek.atomqq.vo.MsgChat;
import org.igeek.atomqq.vo.MsgFriend;
import org.igeek.atomqq.vo.MsgFriendListChange;
import org.igeek.atomqq.vo.MsgFriendStatus;
import org.igeek.atomqq.vo.MsgGroup;
import org.igeek.atomqq.vo.MsgSysGroup;

import android.app.Application;
import android.content.IntentFilter;
import android.util.Log;

/** 
 * 类说明 
 * @author <a href="http://hangxin1940.cnblogs.com">hangxin1940@gmail.com</a>
 * @time 2012-2-2 下午05:34:55 
 */
public class AtomQQApplication extends Application implements OnMessageRecevied{
	
	private static final String LOG_NAME="atomqq";
	
	private boolean sound;
	private boolean shock;
	
	private MessageReceiver messageReceiver;
	
	
	//当前用户
	private User currentUser;
	
	//全部好友
	private Map<Long,Friend> friends;
	
	//好友分组
	private List<FriendCategory> friendCategores;
	
	//群
	private List<Group> groups;
	
	//会话集合
	private Map<Long,List<Chat>> chats;
	
	
	@Override
	public void onCreate() {
		super.onCreate();
		
		friendCategores=Collections.synchronizedList(new ArrayList<FriendCategory>());
		friends=Collections.synchronizedMap(new HashMap<Long, Friend>());
		groups=Collections.synchronizedList(new ArrayList<Group>());
		
		messageReceiver =new MessageReceiver(this);
		
		IntentFilter broadCastFilter=new IntentFilter();
		broadCastFilter.addAction(MessagePoll.BROADCAST_MSG_FRIEND);
		broadCastFilter.addAction(MessagePoll.BROADCAST_MSG_FRIEND_LSTCHG);
		broadCastFilter.addAction(MessagePoll.BROADCAST_MSG_FRIEND_STATUS);
		broadCastFilter.addAction(MessagePoll.BROADCAST_MSG_GROUP);
		broadCastFilter.addAction(MessagePoll.BROADCAST_MSG_SYS_GROUP);
		broadCastFilter.setPriority(1000);
		
		registerReceiver(messageReceiver, broadCastFilter, MessageReceiver.PERMISSION, null);
		
		chats=new LinkedHashMap<Long, List<Chat>>();
		
	}
	
	/**
	 * 增加会话信息
	 * @param uin qq号，群code
	 * @param messages
	 */
	public void putChat(long uin,Chat chat){
		List<Chat> old=chats.get(uin);
		
		if(null==old){
			old=new ArrayList<Chat>();
			old.add(chat);
			chats.put(uin, old);
		}else{
			old.add(chat);
		}
	}
	
	
	/**
	 * 根据uin获得会话
	 * @param uin
	 * @return
	 */
	public List<Chat> getChat(long uin){
		
		List<Chat> chat=chats.get(uin);
		if(null==chat){
			chat=new ArrayList<Chat>();
			chats.put(uin, chat);
		}
		
		return chat;
	}
	
	/**
	 * 根据索引获得会话
	 * @param uin
	 * @return
	 */
	public List<Chat> getChat(int index){
		List<List<Chat>> clist=new ArrayList(chats.values());
		
		
		return clist.get(index);
	}
	
	/**
	 * 获得会话总数
	 * @return
	 */
	public int getChatCount(){
		return chats.size();
	}
	
	/**
	 * 获得当前登录用户
	 * @return
	 */
	public User getCurrentUser(){
		return currentUser;
		
	}
	
	/**
	 * 设置当前登录用户
	 * @param currentUser
	 */
	public void setCurrentUser(User currentUser){
		this.currentUser=currentUser;
	}
	
	/**
	 * 添加一个好友分组
	 * @param fc
	 */
	public void addFriendCategory(FriendCategory friendCategory){
		friendCategores.add(friendCategory);
	}
	
	/**
	 * 添加好友分组
	 * @param friendCategores 好友分组
	 */
	public void setFriendCategores(List<FriendCategory> friendCategores){
		this.friendCategores=friendCategores;
	}
	
	/**
	 * 添加一个好友
	 * @param friend
	 */
	public void addFriend(Friend friend){
		friends.put(friend.getUin(), friend);
	}
	
	/**
	 * 添加好友
	 * @param
	 */
	public void setFriends(Map<Long,Friend> friends){
		this.friends=friends;
	}
	
	/**
	 * 添加一个群
	 * @param gourp
	 */
	public void addGroup(Group gourp){
		groups.add(gourp);
	}
	
	
	/**
	 * 获取一个好友分组
	 * @param index 索引位置
	 * @return
	 */
	public FriendCategory getFriendCategory(int index){
		return friendCategores.get(index);
	}
	
	/**
	 * 获取一个好友
	 * @param uin qq号
	 * @return
	 */
	public Friend getFriend(Long uin){
		return friends.get(uin);
	}
	
	/**
	 * 获取好友分组个数
	 * @return
	 */
	public int getFriendCategoryCount(){
		return friendCategores.size();
	}
	
	/**
	 * 获取一个群
	 * @param index 索引位置
	 * @return
	 */
	public Group getGroup(int index){
		return groups.get(index);
	}
	
	/**
	 * 获取一个群
	 * @param code 群code值
	 * @return
	 */
	public Group getGroup(long code){
		for(Group group:groups){
			if(code==group.getCode())
				return group;
		}
		
		return null;
	}
	
	
	/**
	 * 获取群总数
	 * @return
	 */
	public int getGroupCount(){
		return groups.size();
	}
	
	/**
	 * 设置在线好友
	 * @param onlines
	 */
	public void setOnlineFriends(List<Friend> onlines){
		for(Friend olfriend:onlines){
			
			Friend friend=friends.get(olfriend.getUin());
			friend.setOnline(1);
			friend.setClient_type(olfriend.getClient_type());
			friend.setStatus(olfriend.getStatus());
			
		}
		
		sortFriend();
		
	}
	
	/**
	 * 添加群
	 * @param groups
	 */
	public void setGroups(List<Group> groups){
		this.groups=groups;
	}
	
	/**
	 * 根据qq号对分组成员排序
	 * @param uin
	 */
	private void sortFriend(Long uin){
		int category=friends.get(uin).getCategory();
		
		for(FriendCategory fc:friendCategores){
			if(category==fc.getIndex()){
				fc.sort();
				break;
			}
		}
		
	}
	
	/**
	 * 成员排序
	 * @param uin
	 */
	private void sortFriend(){
		
		for(FriendCategory fc:friendCategores){
			fc.sort();
		}
		
	}
	
	
	
	
	@Override
	public void onTerminate() {
		super.onTerminate();
		
		Log.i("atomqq", "onTerminate");
	}

	@Override
	public void handleMessage(int type, QQMessage message) {
		
		switch (type) {
		case MessageReceiver.BROADCAST_TYPE_FRIEND:
			MsgFriend msgFriend=(MsgFriend) message;
			
			
			
			long uin=msgFriend.getFrom_uin();
			
			friends.get(uin).setNewMessage(1);
			
			sortFriend(uin);
			
			Chat chatFriend=new Chat();
			chatFriend.setType(Chat.FRIEND);
			chatFriend.setUin(uin);
			chatFriend.setMessage(message);
			
			putChat(uin, chatFriend);
			
			Log.i(LOG_NAME, "好友消息");
			for(MsgChat chat:msgFriend.getMessageContents()){
				Log.i(LOG_NAME, "	-"+chat.getContent());
			}
			Log.i(LOG_NAME, "	-");
			
			
			break;

		case MessageReceiver.BROADCAST_TYPE_FRIEND_LISTCHG:
			MsgFriendListChange msgFriendLstChg=(MsgFriendListChange) message;
			
			
			Log.i(LOG_NAME, "好友列表变更");
			for(Friend f:msgFriendLstChg.getAddFriends()){
				
				friends.put(f.getUin(),f);
				getGroup(f.getCategory()).getMembers().put(f.getUin(),f);
				
				Log.i(LOG_NAME, "	增加- "+f.getUin()+" 在分组:  "+f.getCategory());
			}
			for(Friend f:msgFriendLstChg.getRemovedFriends()){
				
				friends.remove(f.getUin());
				getGroup(f.getCategory()).getMembers().remove(f.getUin());
				Log.i(LOG_NAME, "	移除- "+f.getUin());
			}
			Log.i(LOG_NAME, "	");
			
			sortFriend();
			
			break;
			
		case MessageReceiver.BROADCAST_TYPE_FRIEND_STATUS:
			MsgFriendStatus msgFriendStatus=(MsgFriendStatus) message;
			
			Friend f=friends.get(msgFriendStatus.getUin());
			String status=msgFriendStatus.getStatus();
			
			f.setStatus(status);
			if("offline".equals(status)){
				f.setOnline(0);
			}else{
				f.setJustOnline(1);
				f.setOnline(1);
			}
			
			sortFriend(f.getUin());
			
			
			Log.i(LOG_NAME, "好友状态");
			Log.i(LOG_NAME, "	- "+msgFriendStatus.getUin()+" : "+msgFriendStatus.getStatus());
			Log.i(LOG_NAME, "	");
			
			
			
			break;
			
		case MessageReceiver.BROADCAST_TYPE_GROUP:
			MsgGroup msgGroup=(MsgGroup) message;
			
			getGroup(msgGroup.getGroup_code()).setNewMessage(1);
			
			Chat chatGroup=new Chat();
			chatGroup.setType(Chat.GROUP);
			chatGroup.setGcode(msgGroup.getGroup_code());
			chatGroup.setMessage(message);
			
			putChat(chatGroup.getGcode(), chatGroup);
			
			Log.i(LOG_NAME, "群消息");
			for(MsgChat cha1t:msgGroup.getMessageContents()){
				Log.i(LOG_NAME, "	-"+cha1t.getContent());
			}
			
			Log.i(LOG_NAME, "	-");
			
			
			
			break;
			
		case MessageReceiver.BROADCAST_TYPE_SYS_GROUP:
			MsgSysGroup msgSysGroup=(MsgSysGroup) message;
			
		
			
			
			break;

		}
		
	}
	
}
